package com.molichuxing.gateway.bff.goods.convertor;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.gateway.bff.goods.entity.request.create.GoodsExclusiveCreateVo;
import com.molichuxing.gateway.bff.goods.entity.request.create.GoodsSubscribeCreateVo;
import com.molichuxing.gateway.bff.goods.entity.request.create.GoodsSubscribePackageCreateVo;
import com.molichuxing.gateway.bff.goods.entity.response.*;
import com.molichuxing.services.business.dto.request.create.GoodsExclusiveCreateDto;
import com.molichuxing.services.business.dto.request.create.GoodsSubscribeCreateDto;
import com.molichuxing.services.business.dto.request.create.GoodsSubscribePackageCreateDto;
import com.molichuxing.services.business.dto.response.GoodsBaseBizDto;
import com.molichuxing.services.business.dto.response.GoodsBizBizDto;
import com.molichuxing.services.business.dto.response.GoodsDetailBizDto;
import com.molichuxing.services.infrastructure.dto.response.ContractTemplateDto;
import com.molichuxing.services.infrastructure.dto.response.DealerDto;
import com.molichuxing.services.infrastructure.dto.response.GoodsDealerDto;
import com.molichuxing.services.infrastructure.dto.response.GoodsSpecConfigDto;
import com.molichuxing.services.property.GoodsMileageTypeEnum;
import com.molichuxing.services.property.GoodsPaymentTypeEnum;
import com.molichuxing.services.property.GoodsTypeEnum;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @prouect_name: op-gateway
 * @class_name: ${CLASS_NAME}
 * @description: ${DESCRIPTION}
 * @author: 林亿建
 * @create_date: 2019年08月01日
 * @modify_date: 2019年08月01日
 **/
public class Convert {
    /********************************************商品属性信息转换 开始 *****************************/
    public static Paged<GoodsVo> toGoodsPage(Paged<GoodsBizBizDto> page, Map<Integer, String> vehicleMap, Map<Integer, String> vehicleImgMap) {
        Paged<GoodsVo> result = null;;
        if(null != page) {
            result = new Paged<>();
            BeanUtils.copyProperties(page, result);
            result.setList(toGoodsVo(page.getList(), vehicleMap, vehicleImgMap));
        }
        return result;
    }

    public static List<GoodsVo> toGoodsVo(List<GoodsBizBizDto> beanList, Map<Integer, String> vehicleMap, Map<Integer, String> vehicleImgMap) {
        List<GoodsVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (GoodsBizBizDto bean : beanList) {
                dtoList.add(toGoodsVo(bean, vehicleMap, vehicleImgMap));
            }
        }
        return dtoList;
    }

    public static GoodsVo toGoodsVo(GoodsBizBizDto bean, Map<Integer, String> vehicleMap, Map<Integer, String> vehicleImgMap) {
        GoodsVo beanDto = null;
        if (bean != null) {
            beanDto = new GoodsVo();
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setPaymentType(bean.getPaymentType());
            beanDto.setPaymentTypeStr(bean.getPaymentTypeStr());
            if(null != vehicleMap && (null != vehicleMap.get(bean.getGoodsId()))) {
                beanDto.setVehicleStyleStr(vehicleMap.get(bean.getGoodsId()));
            }
            if(null != vehicleImgMap && (null != vehicleImgMap.get(bean.getGoodsId()))) {
                beanDto.setVehicleImg(vehicleImgMap.get(bean.getGoodsId()));
            }
            beanDto.setUseVehicleTime(bean.getUseVehicleTime()+"");
        }
        return beanDto;
    }
    /********************************************商品信息转换 结束 *****************************/

    /********************************************商品基础信息转换 开始 *****************************/
    public static GoodsBaseVo toGoodsBaseVo(GoodsBaseBizDto bean, String goodsPicture) {
        GoodsBaseVo beanDto = null;
        if (bean != null) {
            beanDto = new GoodsBaseVo();
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setGoodsPicture(goodsPicture);
            beanDto.setUseVehicleTime(bean.getUseVehicleTime()+"");
        }
        return beanDto;
    }

    public static GoodsExclusiveCreateDto toGoodsExclusiveCreate(GoodsExclusiveCreateVo bean) {
        GoodsExclusiveCreateDto beanDto = null;
        if (bean != null) {
            beanDto = new GoodsExclusiveCreateDto();
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setPaymentType(GoodsPaymentTypeEnum.getEnum(bean.getPaymentType()));
            beanDto.setType(GoodsTypeEnum.EXCLUSIVE);
        }
        return beanDto;
    }

    public static GoodsSubscribeCreateDto toGoodsSubscribeCreate(GoodsSubscribeCreateVo bean) {
        GoodsSubscribeCreateDto beanDto = null;
        if (bean != null) {
            beanDto = new GoodsSubscribeCreateDto();
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setPaymentType(GoodsPaymentTypeEnum.getEnum(bean.getPaymentType()));
            beanDto.setGoodsPackage(toGoodsSubscribePackageCreateDto(bean.getGoodsPackage()));
            beanDto.setType(GoodsTypeEnum.SUBSCRIBE);
        }
        return beanDto;
    }



    public static List<GoodsSubscribePackageCreateDto> toGoodsSubscribePackageCreateDto(List<GoodsSubscribePackageCreateVo> beanList) {
        List<GoodsSubscribePackageCreateDto> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (GoodsSubscribePackageCreateVo bean : beanList) {
                dtoList.add(toGoodsSubscribePackageCreateDto(bean));
            }
        }
        return dtoList;
    }

    public static GoodsSubscribePackageCreateDto toGoodsSubscribePackageCreateDto(GoodsSubscribePackageCreateVo bean) {
        GoodsSubscribePackageCreateDto beanDto = null;
        if (bean != null) {
            beanDto = new GoodsSubscribePackageCreateDto();
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setPackageType(GoodsMileageTypeEnum.getEnum((byte)bean.getPackageType()));
        }
        return beanDto;
    }
    /********************************************商品基础信息转换 结束 *****************************/

    /********************************************商品详细信息转换 开始 *****************************/
    public static GoodsDetailVo toGoodsDetailVo(GoodsDetailBizDto bean) {
        GoodsDetailVo beanDto = null;
        if (bean != null) {
            beanDto = new GoodsDetailVo();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }


    public static List<GoodsSpecConfigVo> toGoodsSpecConfigVo(List<GoodsSpecConfigDto> beanList) {
        List<GoodsSpecConfigVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (GoodsSpecConfigDto bean : beanList) {
                dtoList.add(toGoodsSpecConfigVo(bean));
            }
        }
        return dtoList;
    }

    public static GoodsSpecConfigVo toGoodsSpecConfigVo(GoodsSpecConfigDto bean) {
        GoodsSpecConfigVo beanDto = null;
        if (bean != null) {
            beanDto = new GoodsSpecConfigVo();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }

    /********************************************商品详细信息转换 结束 *****************************/


    /********************************************授权分销商信息转换 开始 *****************************/
    public static Paged<GoodsDealerVo> toGoodsDealerPage(Paged<GoodsDealerDto> page, Map<Integer, DealerDto> dealerList, Map<Integer, String> regionNameMap) {
        Paged<GoodsDealerVo> result = null;;
        if(null != page) {
            result = new Paged<>();
            BeanUtils.copyProperties(page, result);
            result.setList(toGoodsDealerVo(page.getList(), dealerList, regionNameMap));
        }
        return result;
    }

    public static List<GoodsDealerVo> toGoodsDealerVo(List<GoodsDealerDto> beanList, Map<Integer, DealerDto> dealerList, Map<Integer, String> regionNameMap) {
        List<GoodsDealerVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (GoodsDealerDto bean : beanList) {
                dtoList.add(toGoodsDealerVo(bean, dealerList, regionNameMap));
            }
        }
        return dtoList;
    }

    public static GoodsDealerVo toGoodsDealerVo(GoodsDealerDto bean, Map<Integer, DealerDto> dealerList, Map<Integer, String> regionNameMap) {
        GoodsDealerVo beanDto = null;
        if (bean != null) {
            beanDto = new GoodsDealerVo();
            BeanUtils.copyProperties(bean, beanDto);
            if((null != dealerList)&&(null != dealerList.get(bean.getDealerId()))) {
                DealerDto dealerDto = dealerList.get(bean.getDealerId());
                beanDto.setDealerName(dealerDto.getShortName());
                if((null != regionNameMap)&&(null != regionNameMap.get(dealerDto.getRegionId()))) {
                    beanDto.setAddress(regionNameMap.get(dealerDto.getRegionId()));
                }
            }
        }
        return beanDto;
    }





    public static List<GoodsDealerVo> toGoodsDealerListVo(List<DealerDto> beanList, List<GoodsDealerDto>  dealerList, Map<Integer, String> regionNameMap) {
        List<GoodsDealerVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (DealerDto bean : beanList) {
                dtoList.add(toGoodsDealerListVo(bean, dealerList, regionNameMap));
            }
        }
        return dtoList;
    }

    public static GoodsDealerVo toGoodsDealerListVo(DealerDto bean, List<GoodsDealerDto>  dealerList, Map<Integer, String> regionNameMap) {
        GoodsDealerVo beanDto = null;
        if (bean != null) {
            beanDto = new GoodsDealerVo();
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setStatus(1);
            if((null != dealerList)&&(dealerList.size() > 0)) {
                for(GoodsDealerDto goodsDealerDto : dealerList) {
                    if(goodsDealerDto.getDealerId().equals(bean.getDealerId())) {
                        beanDto.setStatus(0);
                        beanDto.setGoodsId(goodsDealerDto.getGoodsId());
                    }
                }
            }
            beanDto.setDealerName(bean.getShortName());
        }
        return beanDto;
    }


    public static List<ContractTemplateVo> toContractTemplateVo(List<ContractTemplateDto> beanList) {
        List<ContractTemplateVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (ContractTemplateDto bean : beanList) {
                dtoList.add(toContractTemplateVo(bean));
            }
        }
        return dtoList;
    }

    public static ContractTemplateVo toContractTemplateVo(ContractTemplateDto bean) {
        ContractTemplateVo beanDto = null;
        if (bean != null) {
            beanDto = new ContractTemplateVo();
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setTemplatePath(bean.getTemplateOssPath());
        }
        return beanDto;
    }

    /********************************************授权分销商信息转换 结束 *****************************/
}
